package com.test.platform.ui.screen

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.test.platform.model.DeveloperInfo

// 添加必要的导入
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.lifecycle.viewmodel.compose.viewModel
import com.test.platform.network.RetrofitClient
import com.test.platform.repository.DeveloperInfoRepository
import com.test.platform.viewmodel.DeveloperInfoViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DeveloperInfoScreen(
    modifier: Modifier = Modifier,
    viewModel: DeveloperInfoViewModel = viewModel {
        DeveloperInfoViewModel(DeveloperInfoRepository(RetrofitClient.apiService))
    }
) {
    val developerInfos by viewModel.developerInfos.collectAsState()
    val loading by viewModel.loading.collectAsState()
    var errorMessage by remember { mutableStateOf<String?>(null) }
    
    LaunchedEffect(Unit) {
        try {
            viewModel.loadDeveloperInfos()
        } catch (e: Exception) {
            errorMessage = e.message ?: "加载失败"
        }
    }
    
    Scaffold(
        modifier = modifier,
        topBar = {
            TopAppBar(
                title = { Text("开发信息") }
            )
        }
    ) { padding ->
        if (loading) {
            CircularProgressIndicator(
                modifier = Modifier
                    .fillMaxSize()
                    .wrapContentSize()
            )
        } else if (errorMessage != null) {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(padding)
                    .padding(16.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Text(
                    text = errorMessage ?: "",
                    color = MaterialTheme.colorScheme.error
                )
                Spacer(modifier = Modifier.height(16.dp))
                Button(
                    onClick = {
                        errorMessage = null
                        viewModel.loadDeveloperInfos()
                    }
                ) {
                    Text("重试")
                }
            }
        } else {
            LazyColumn(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(padding)
            ) {
                item {
                    TableHeader()
                }
                items(developerInfos) { info ->
                    DeveloperInfoRow(info)
                }
            }
        }
    }
}

@Composable
private fun TableHeader() {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp)
    ) {
        Text("组织", modifier = Modifier.weight(1f))
        Text("库", modifier = Modifier.weight(1f))
        Text("开发者", modifier = Modifier.weight(1f))
        Text("PR标题", modifier = Modifier.weight(1.5f))
    }
}

@Composable
private fun DeveloperInfoRow(info: DeveloperInfo) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp)
    ) {
        Text(info.owner, modifier = Modifier.weight(1f))
        Text(info.name, modifier = Modifier.weight(1f))
        Text(info.developer, modifier = Modifier.weight(1f))
        Text(info.title ?: "", modifier = Modifier.weight(1.5f))
    }
}

@Composable
private fun DeveloperInfoCard(info: DeveloperInfo) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(8.dp)
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Column {
                    Text(
                        text = "${info.owner}/${info.name}",
                        style = MaterialTheme.typography.titleMedium
                    )
                    Text(
                        text = info.developer,
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.primary
                    )
                }
                IconButton(onClick = { /* TODO: 更多操作 */ }) {
                    Icon(Icons.Default.MoreVert, "更多")
                }
            }
            
            Spacer(modifier = Modifier.height(8.dp))
            
            Text(
                text = info.title ?: "",
                style = MaterialTheme.typography.bodyMedium
            )
            
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.End
            ) {
                TextButton(onClick = { /* TODO: 查看PR */ }) {
                    Text("查看PR")
                }
            }
        }
    }
}